package com.atguigu.tingshu.user.config.jwt;

import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import org.springframework.stereotype.Component;

import java.security.KeyPair;

/**
 * @author Haoran Sun
 * @since 2024/08/16
 */

@Component
public class JwtConfig {

    @Bean
    public JwtConstantsProperties jwtConstantsProperties() {
        ClassPathResource classPathResource = new ClassPathResource("tingshu.jks");
        //获取文件中的私钥
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(classPathResource, "tingshu".toCharArray());
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("tingshu", "tingshu".toCharArray());
        JwtConstantsProperties jwtConstantsProperties = new JwtConstantsProperties();
        jwtConstantsProperties.setPrivateKey(keyPair.getPrivate());
        jwtConstantsProperties.setPublicKey(keyPair.getPublic());
        return jwtConstantsProperties;
    }

}
